package Plugin_Reprise.editors.my_sql_editor;

import java.sql.DriverManager;
import java.util.Vector;

import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;

import Plugin_Reprise.Activator;
import Plugin_Reprise.preferences.PreferenceConstants;

public class SQLPartitionner extends FastPartitioner {

	public SQLPartitionner(IPartitionTokenScanner scanner,
			String[] legalContentTypes) {
		
		
		super(scanner, legalContentTypes);
		// TODO Auto-generated constructor stub
	}
@Override
public void connect(IDocument document, boolean delayInitialization) {
	// TODO Auto-generated method stub
	super.connect(document, delayInitialization);
	GetSqls(document);
}

public Vector<String> GetSqls(IDocument document)
{

    StringBuffer commentaires = new StringBuffer();
    StringBuffer sql = new StringBuffer();
    Vector<String> sqls=new Vector<String>();
    Vector<String> results=new Vector<String>();
    ITypedRegion[] partitions = computePartitioning(0, document.getLength());
    
    for (int i = 0; i < partitions.length; i++)
    {
        try
        {
        	if (partitions[i].getType()==EditorPartitionScannerSQL.SQL_COMMENT
        			){
        		commentaires.append(document.get(partitions[i].getOffset(),partitions[i].getLength()));
        		commentaires.append('\n');
        	}else if (partitions[i].getType()==EditorPartitionScannerSQL.SQL_ALIAS){
        		String aliasdetecte=document.get(partitions[i].getOffset(),partitions[i].getLength());
    			IPreferenceStore prefs =Activator.getDefault().getPreferenceStore();
        		if (aliasdetecte.equals("&erp.")) sql.append(prefs.getString(PreferenceConstants.Oracle_Constants.ORA_SHEMAERP ));
        		if (aliasdetecte.equals("&terp.")) sql.append(prefs.getString(PreferenceConstants.Oracle_Constants.ORA_SHEMATERP ));
        		if (aliasdetecte.equals("&outils.")) sql.append(prefs.getString(PreferenceConstants.Oracle_Constants.ORA_SHEMAOUTILS ));
        		if (aliasdetecte.equals("&toutils.")) sql.append(prefs.getString(PreferenceConstants.Oracle_Constants.ORA_SHEMATOUTILS ));
        		
        	}else if (partitions[i].getType()==EditorPartitionScannerSQL.SQL_TableUV){
        		String aliasdetecte=document.get(partitions[i].getOffset(),partitions[i].getLength());
        		System.out.println("Table : " + aliasdetecte);
        		sql.append(aliasdetecte+" ");
        		
        	
        	}else if (partitions[i].getType()==EditorPartitionScannerSQL.SQL_TAG ){
        		String textesql=document.get(partitions[i].getOffset(),partitions[i].getLength());
        		
        		sql.append(textesql);
        	}else if (partitions[i].getType()==EditorPartitionScannerSQL.SQL_SEPARATOR){
        		
        		sqls.add(sql+"");
        		
        		sql = new StringBuffer();
        	}
           /* buffer.append("Partition type: " 
              + partitions[i].getType() 
              + ", offset: " + partitions[i].getOffset()
              + ", length: " + partitions[i].getLength());
            buffer.append("\n");
            buffer.append("Text:\n");
            buffer.append(document.get(partitions[i].getOffset(), 
             partitions[i].getLength()));
            buffer.append("\n---------------------------\n\n\n");*/
        }
        catch (BadLocationException e)
        {
           /* e.printStackTrace();*/
        }
    }
    sqls.add(sql+"");
    for (String req:sqls){
    	String requete=req.replaceAll("\r\n", " ").replaceAll("\n", " ").trim();
    	if (!requete.equals("")){
	    	System.out.println("------------");
	    	System.out.println(requete);   
	    	results.add(requete);
    	}

    }
    return results;
    /*System.out.print(buffer);*/
}


}
